package 算法.leetcode.labuladong.数组和链表;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2022/6/18
 */
public class 每日温度 {
    class Solution {
        public int[] dailyTemperatures(int[] temperatures) {
            int[] ans = new int[temperatures.length];
            // 索引栈
            Stack<Integer> s = new Stack<>();
            for (int i = temperatures.length-1;i>=0;i--){
                while (!s.isEmpty()&&temperatures[s.peek()]<=temperatures[i]){
                    s.pop();
                }
                // 得到索引间距
                ans[i] = s.isEmpty()?-1:(s.peek()-i);
                // 索引入栈
                s.push(i);
            }
            return ans;
        }
    }
}
